package cn.yhz.pethome.dept.controller;

import cn.yhz.pethome.base.util.AjaxResult;
import cn.yhz.pethome.base.util.ResultBean;
import cn.yhz.pethome.dept.Query.EmployeeQuery;
import cn.yhz.pethome.dept.domain.Employee;
import cn.yhz.pethome.dept.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.selectById(id);
    }

    /**
     * 查询去全部
     * @return
     */
    @GetMapping
    public List<Employee> queryEmp(){
        return employeeService.selectEmp();
    }

    /**
     * 查询所有和高级查询
     * @param employeeQuery
     * @return
     */
    @PostMapping
    public ResultBean<Employee> queryAll(@RequestBody EmployeeQuery Query){
        return employeeService.queryAll(Query);
    }

    /**
     * 新增和修改
     * @param employee
     * @return
     */
    @PutMapping
    public AjaxResult insert(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.insert(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult dept(@RequestBody List<Long>ids){
        try {
            employeeService.batchEmp(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
}
